using UnityEngine;
using System.Linq;
using System.Collections.Generic;
using System;

public class cameraholder : MonoBehaviour
{
    public Transform[] targets;
    public Transform redSpaceship;
    public Transform blueSpaceship;

    private Vector3 centerPoint;
    private Vector3 cameraOffset;

    void Start()
    {
        Transform[] targets = new Transform[] { redSpaceship, blueSpaceship };
		
        //计算目标物体之间的距离和相对位置
        var bounds = new Bounds(targets[0].position, Vector3.zero);
        for (int i = 0; i < targets.Length; i++)
            bounds.Encapsulate(targets[i].position);
        centerPoint = bounds.center;
        //计算相机的偏移量
        cameraOffset = transform.position - centerPoint;
    }

    void Update()
    {
        //重新计算目标物体之间的距离和相对位置（以防两个目标物体在运动）
        var bounds = new Bounds(targets[0].position, Vector3.zero);
        for (int i = 0; i < targets.Length; i++)
            bounds.Encapsulate(targets[i].position);
        centerPoint = bounds.center;

        //将相机的位置设置为中心点加上相机偏移量。
        transform.position = centerPoint + cameraOffset;
    }
}
